<template>
  <div class="content">
    <div class="content__left">
      <ul>
        <!-- 根据leftList循环渲染左侧tab按钮，并且在被点击后添加‘cative’class名 -->
        <li
          :class="{
            content__left__item: true,
            active: item.tab === currentTab,
          }"
          v-for="item in leftList"
          :key="item.tab"
          @click="handleClick(item.tab)"
        >
          {{ item.tittle }}
        </li>
      </ul>
    </div>
    <div class="content__right">
      <ul>
        <!-- 根据contentList的内容循环渲染组件 -->
        <li
          class="content__right__item"
          v-for="item in contentList"
          :key="item._id"
        >
          <img :src="item.imgUrl" alt="" class="item__img" />
          <div class="item__info">
            <p class="info__tittle">{{ item.name }}</p>
            <p class="info__sales">月售{{ item.sales }}件</p>
            <p class="info__price">
              <span class="info__price__yen">&yen;</span>
              <span class="info__price__current">{{ item.price }}</span>
              <span class="info__price__origin">&yen;{{ item.oldPrice }}</span>
            </p>
          </div>
          <!-- 点击‘+’或‘-’按钮后执行changeCart方法 -->
          <div class="item__order">
            <span
              class="order__minus iconfont"
              @click="changeCart(shopId, item._id, item, -1,shopName)"
              >&#xe640;</span
            >
            <span class="order__num">{{ cartList?.[shopId]?.productList?.[item._id]?.count || 0 }}</span>
            <span
              class="order__plus iconfont"
              @click="changeCart(shopId, item._id, item, 1,shopName)"
              >&#xe667;</span
            >
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { reactive, toRefs, ref, watchEffect } from "vue";
import { useRoute } from "vue-router";
import { get } from "../../utils/post";
import { useCart } from "./useCart";

const useHandleClick = () => {
  // 设置currentTab的值为‘all’
  const currentTab = ref("all");
  // 设置点击tab按钮事件
  const handleClick = (tab) => {
    // 将currentTab的值设置为被点击按钮的tab值
    currentTab.value = tab;
    console.log(currentTab.value);
  };
  return { handleClick, currentTab };
};

const useData = (currentTab, shopId) => {
  const data = reactive({ contentList: [] });
  // 设置getData方法，接收当前tab的商品信息
  const getData = async () => {
    const result = await get(`/api/shop/${shopId}/products`, {
      tab: currentTab.value,
    });
    if (result?.errno === 0 && result?.data?.length) {
      // 成功接收到返回数据后将数据赋值给data.contentList
      data.contentList = result.data;
    }
  };
  // 给getData方法设置侦听器
  watchEffect(() => {
    getData();
  });
  const { contentList } = toRefs(data);
  return { contentList };
};

export default {
  name: "Content",
  props:['shopName'],
  setup() {
    // 设置leftList常量
    const leftList = reactive([
      { tab: "all", tittle: "全部商品" },
      { tab: "seckill", tittle: "秒杀" },
      { tab: "fruit", tittle: "新鲜水果" },
    ]);
    const route = useRoute();
    // 设置shopId为当前页面的id
    const shopId = route.params.id;
    const { handleClick, currentTab } = useHandleClick();
    const { contentList } = useData(currentTab, shopId);
    const { changeCart,cartList } = useCart();
    return {
      leftList,
      contentList,
      handleClick,
      currentTab,
      shopId,
      changeCart,
      cartList,
    };
  },
};
</script>

<style lang="scss" scoped>
@import "../../style/viriables";
@import "../../style/mixins";
.content {
  ul {
    list-style: none;
    padding: 0;
    margin: 0;
  }
  p {
    margin: 0;
    padding: 0;
  }
  position: absolute;
  top: 1.58rem;
  bottom: 0.5rem;
  left: 0;
  right: 0;
  display: flex;
  &__left {
    width: 0.76rem;
    height: 100%;
    overflow-y: auto;
    background-color: $input-bgcolor;
    &__item {
      height: 0.4rem;
      text-align: center;
      font-size: 0.14rem;
      line-height: 0.4rem;
      color: $content-fontcolor;
    }
    .active {
      background-color: #fff;
    }
  }
  &__right {
    flex: 1;
    padding-left: 0.16rem;
    padding-right: 0.18rem;
    overflow-y: auto;
    &__item {
      padding-top: 0.08rem;
      padding-bottom: 0.12rem;
      border-bottom: 1px solid $content-bgcolor;
      display: flex;
      position: relative;
      .item__img {
        width: 0.68rem;
        height: 0.68rem;
        margin-right: 0.16rem;
      }
      .item__info {
        overflow: hidden;
        .info__tittle {
          height: 0.2rem;
          line-height: 0.2rem;
          font-weight: bold;
          font-size: 0.14rem;
          color: $content-fontcolor;
          margin-bottom: 0.06rem;
          @include single-ellipsis;
        }
        .info__sales {
          height: 0.16rem;
          line-height: 0.16rem;
          color: $content-fontcolor;
          margin-bottom: 0.06rem;
        }
        .info__price {
          &__yen {
            font-size: 0.12rem;
            color: $content-highlightcolor;
          }
          &__current {
            font-size: 0.14rem;
            line-height: 0.2rem;
            color: $content-highlightcolor;
            margin-right: 0.06rem;
          }
          &__origin {
            font-size: 0.12rem;
            line-height: 0.2rem;
            color: #999;
          }
        }
      }
      .item__order {
        position: absolute;
        bottom: 0.12rem;
        right: 0;
        .order__minus,
        .order__plus {
          font-size: 0.2rem;
        }
        .order__minus {
          color: #666;
          margin-right: 0.1rem;
        }
        .order__plus {
          color: $btn-bgcolor;
        }
        .order__num {
          display: inline-block;
          width: 0.16rem;
          text-align: center;
          margin-right: 0.1rem;
          color: $content-fontcolor;
          font-size: 0.14rem;
          line-height: 0.2rem;
        }
      }
    }
  }
}
</style>